//
//  GFNearHomeCollectionViewCell.m
//  GoodFood
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFNearHomeCollectionViewCell.h"

@interface GFNearHomeCollectionViewCell ()

@property (nonatomic, strong) UIImageView *foodImageView;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UILabel *foodNameLabel;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *timeImage;
@property (nonatomic, strong) UILabel *pvCountlLabel;
@property (nonatomic, strong) UIImageView *pvImage;
@property (nonatomic, strong) UILabel *commentLabel;
@property (nonatomic, strong) UILabel *commentCountLabel;
@property (nonatomic, strong) UIImageView *commentCountImage;
@property (nonatomic, strong) UILabel *placeNameLabel;
@property (nonatomic, strong) UILabel *wantLabel;
@property (nonatomic, strong) UIImageView *wantImage;
@property (nonatomic, strong) UIView *userView;
@property (nonatomic, strong) UILabel *distanceLabel;
@property (nonatomic, strong) UIImageView *distanceImage;

@end

@implementation GFNearHomeCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:1.000 green:0.978 blue:0.977 alpha:1.000];
        
        self.userView = [[UIView alloc] init];
        self.userView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.userView];
        
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [tap addTarget:self action:@selector(tap:)];
        [self.userView addGestureRecognizer:tap];
        
        self.foodImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self.contentView addSubview:self.foodImageView];
        
        self.userImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.userImageView];
        
        self.foodNameLabel = [[UILabel alloc] init];
        self.foodNameLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.foodNameLabel];
        
        self.userNameLabel = [[UILabel alloc] init];
        self.userNameLabel.font = [UIFont systemFontOfSize:13];
        self.userNameLabel.font = [UIFont boldSystemFontOfSize:15];
        self.userNameLabel.textColor = [UIColor colorWithRed:1.000 green:0.448 blue:0.636 alpha:0.780];
        [self.contentView addSubview:self.userNameLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.timeLabel];
        
        self.timeImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.timeImage];
        
        self.pvCountlLabel = [[UILabel alloc] init];
        self.pvCountlLabel.textAlignment = NSTextAlignmentCenter;
        self.pvCountlLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.pvCountlLabel];
        
        self.pvImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.pvImage];
        
        self.commentLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.commentLabel];
        
        self.commentCountLabel = [[UILabel alloc] init];
        self.commentCountLabel.textAlignment = NSTextAlignmentCenter;
        self.commentCountLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.commentCountLabel];
        
        self.commentCountImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.commentCountImage];
        
        self.placeNameLabel = [[UILabel alloc] init];
        self.placeNameLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.placeNameLabel];
        
        self.wantLabel = [[UILabel alloc] init];
        self.wantLabel.textAlignment = NSTextAlignmentCenter;
        self.wantLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.wantLabel];
        
        self.wantImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.wantImage];
        
        self.distanceImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.distanceImage];
        
        self.distanceLabel = [[UILabel alloc] init];
        self.distanceLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.distanceLabel];
        
        
    }
    return self;
}

-(void)setModel:(GFNearHomeModel *)model {
    
    if (_model != model) {
        _model = model;
    }
    
    //根据请求的照片大小计算imageView的高度
    CGFloat h = self.frame.size.width * ([model.image_height floatValue] / 5) / ((ScreenWidth - 15) / 2);
    
    self.foodImageView.frame = CGRectMake(0, 0, (ScreenWidth - 15) / 2, h);
    
    [self.foodImageView setImageWithURL:[NSURL URLWithString:self.model.picture_url] placeholderImage:[UIImage imageNamed:@"category_1"]];
    
    self.foodNameLabel.frame = CGRectMake(0, h, (ScreenWidth - 15) / 2, ScreenWidth / 15);
    self.foodNameLabel.text = model.food_name;
    
    if (model.distance == nil) {
        self.timeImage.frame = CGRectMake(0, h + ScreenWidth / 15, 15, 15);
        self.timeImage.image = [UIImage imageNamed:@"iconfont-llstoredetailtime"];
        
        self.timeLabel.frame = CGRectMake(15, h + ScreenWidth / 15, ScreenWidth / 5, 15);
        NSString *time = [model.publish_time substringToIndex:10];
        self.timeLabel.text = time;
    } else {
        
        self.distanceImage.frame = CGRectMake(0, h + ScreenWidth / 15, 15, 15);
        self.distanceImage.image = [UIImage imageNamed:@"iconfont-julichi-0"];
        self.distanceLabel.frame = CGRectMake(20, h + ScreenWidth / 15, ScreenWidth / 5, 15);
        if ([model.distance integerValue] <= 100) {
            self.distanceLabel.text = @"100米以内";
        } else if ([model.distance integerValue] > 100 && [model.distance integerValue] <= 1000) {
            self.distanceLabel.text = [NSString stringWithFormat:@"%ld米", [model.distance integerValue]];
        } else {
            self.distanceLabel.text = [NSString stringWithFormat:@"%.1f公里", [model.distance floatValue] / 1000];
        }
        
        
    }
    
    
    //判断有没有想要 足迹和评论
    if ([model.want_it_total integerValue] == 0) {
        //如果没有想要就把想要的Label从父视图中移除
        [self.wantImage removeFromSuperview];
        [self.wantLabel removeFromSuperview];
        
        if ([model.pv_count integerValue] == 0) {
            [self.pvImage removeFromSuperview];
            [self.pvCountlLabel removeFromSuperview];
            
            if ([model.comment_count integerValue] == 0) {
                [self.commentCountImage removeFromSuperview];
                [self.commentCountLabel removeFromSuperview];
            } else {
                
                [self.contentView addSubview:self.commentCountImage];
                [self.contentView addSubview:self.commentCountLabel];
                
                self.commentCountImage.frame = CGRectMake(0, h + ScreenWidth / 15 + 15 , 15, 15);
                self.commentCountImage.image = [UIImage imageNamed:@"iconfont-pinglun"];
                self.commentCountLabel.frame = CGRectMake(15, h + ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
                self.commentCountLabel.text = [model.comment_count stringValue];
            }
        } else {
            
            [self.contentView addSubview:self.pvImage];
            [self.contentView addSubview:self.pvCountlLabel];
            
            self.pvImage.frame = CGRectMake(0, h + ScreenWidth / 15 + 15 , 15, 15);
            self.pvImage.image = [UIImage imageNamed:@"iconfont-liulan"];
            self.pvCountlLabel.frame = CGRectMake(15, h +ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
            self.pvCountlLabel.text = [model.pv_count stringValue];
            
            if ([model.comment_count integerValue] == 0) {
                [self.commentCountImage removeFromSuperview];
                [self.commentCountLabel removeFromSuperview];
                
            } else {
                
                [self.contentView addSubview:self.commentCountImage];
                [self.contentView addSubview:self.commentCountLabel];
                
                self.commentCountImage.frame = CGRectMake(15 + ScreenWidth / 15, h + ScreenWidth / 15 + 15 , 15, 15);
                self.commentCountImage.image = [UIImage imageNamed:@"iconfont-pinglun"];
                
                self.commentCountLabel.frame = CGRectMake(15 + ScreenWidth / 15 + 15, h + ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
                self.commentCountLabel.text = [model.comment_count stringValue];
                
            }
        }
    } else {
        //如果有想要就把想要的label加在self.contentView上
        [self.contentView addSubview:self.wantImage];
        [self.contentView addSubview:self.wantLabel];
        self.wantImage.frame = CGRectMake(0, h + ScreenWidth / 15 + 15 , 15, 15);
        self.wantImage.image = [UIImage imageNamed:@"iconfont-xihuan-2"];
        self.wantLabel.frame = CGRectMake(15, h + ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
        self.wantLabel.text = [model.want_it_total stringValue];
        
        if ([model.pv_count integerValue] == 0) {
            [self.pvImage removeFromSuperview];
            [self.pvCountlLabel removeFromSuperview];
            
            if ([model.comment_count integerValue] == 0) {
                [self.commentCountImage removeFromSuperview];
                [self.commentCountLabel removeFromSuperview];
            } else {
                
                [self.contentView addSubview:self.commentCountImage];
                [self.contentView addSubview:self.commentCountLabel];
                
                self.commentCountImage.frame = CGRectMake(15 + ScreenWidth / 15, h + ScreenWidth / 15 + 15 , 15, 15);
                self.commentCountImage.image = [UIImage imageNamed:@"iconfont-pinglun"];
                self.commentCountLabel.frame = CGRectMake(15 + ScreenWidth / 15 + 15, h + ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
                self.commentCountLabel.text = [model.comment_count stringValue];
            }
        } else {
            
            [self.contentView addSubview:self.pvImage];
            [self.contentView addSubview:self.pvCountlLabel];
            
            self.pvImage.frame = CGRectMake(15 + ScreenWidth / 15, h + ScreenWidth / 15 + 15 , 15, 15);
            self.pvImage.image = [UIImage imageNamed:@"iconfont-liulan"];
            self.pvCountlLabel.frame = CGRectMake(15 + ScreenWidth / 15 + 15, h +ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
            self.pvCountlLabel.text = [model.pv_count stringValue];
            
            if ([model.comment_count integerValue] == 0) {
                [self.commentCountImage removeFromSuperview];
                [self.commentCountLabel removeFromSuperview];
            } else {
                
                [self.contentView addSubview:self.commentCountImage];
                [self.contentView addSubview:self.commentCountLabel];
                
                self.commentCountImage.frame = CGRectMake(15 + (ScreenWidth / 15) * 2 + 15, h + ScreenWidth / 15 + 15 , 15, 15);
                self.commentCountImage.image = [UIImage imageNamed:@"iconfont-pinglun"];
                
                self.commentCountLabel.frame = CGRectMake(15 + (ScreenWidth / 15) * 2 + 30, h + ScreenWidth / 15 + 15, ScreenWidth / 15, 15);
                self.commentCountLabel.text = [model.comment_count stringValue];
                
            }
        }
    }
    
    self.userView.frame = CGRectMake(0, h + ScreenWidth / 15 + 15 + 15, (ScreenWidth - 15) / 2, (ScreenWidth - 15) / 6);
    
    self.userImageView.frame = CGRectMake(ScreenWidth / 30, h + ScreenWidth / 15 + 15 + 15 + ScreenWidth / 30, ScreenWidth / 10, ScreenWidth / 10);
    [self.userImageView setImageWithURL:[NSURL URLWithString:model.user_image] placeholderImage:[UIImage imageNamed:@"category_1"]];
    
    self.userNameLabel.frame = CGRectMake(ScreenWidth / 30 + ScreenWidth / 10 + 10, h + ScreenWidth / 15 + 15 + 15 + ScreenWidth / 30, (ScreenWidth - 15) / 2 - ScreenWidth / 30 - ScreenWidth / 10 - 10, ScreenWidth / 20);
    self.userNameLabel.text = model.user_name;
    
    self.placeNameLabel.frame = CGRectMake(ScreenWidth / 30 + ScreenWidth / 10 + 10, h + ScreenWidth / 15 + 15 + 15 + ScreenWidth / 30 + ScreenWidth / 20, (ScreenWidth - 15) / 2 - ScreenWidth / 30 - ScreenWidth / 10 - 10, ScreenWidth / 20);
    if (model.city_name != nil) {
        self.placeNameLabel.text = [model.city_name stringByAppendingFormat:@"@%@",model.place_name];
    } else {
        
        self.placeNameLabel.text = [NSString stringWithFormat:@"@%@", model.place_name];
    }
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap {
    NSLog(@"ssss");
    self.block();
    
}

@end
